package com.example.app2;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class ShareWriteActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ckb_married;
    private SharedPreferences preference;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharewrite);
        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ckb_married = findViewById(R.id.ckb_married);
        findViewById(R.id.btn_save).setOnClickListener(this);
        preference = getSharedPreferences("config", Context.MODE_PRIVATE);
        String name = preference.getString("name", null);
        if (name != null) {
            et_name.setText(name);
        }
        int age = preference.getInt("age", 0);
        if (age != 0) {
            et_age.setText(String.valueOf(age));

        }
        double height=preference.getFloat("height",0);
        if(height!=0)
        {
            et_height.setText(String.valueOf(height));
        }
        double  weight=preference.getFloat("weight",0);
        if(weight!=0)
        {
            et_weight.setText(String.valueOf(weight));
        }
        ckb_married.setChecked(preference.getBoolean("married",false));
    }

    @Override
    public void onClick(View v) {
        //保存信息
        SharedPreferences.Editor editor = preference.edit();
        editor.putString("name", et_name.getText().toString());
        editor.putInt("age", Integer.parseInt(et_age.getText().toString()));
        editor.putFloat("height", Float.parseFloat(et_height.getText().toString()));
        editor.putFloat("weight", Float.parseFloat(et_weight.getText().toString()));
        editor.putBoolean("married", ckb_married.isChecked());
        editor.commit();//提交保存
    }
}
